package com.maas.util.mybatis.spring;

import java.io.IOException;

import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.SqlSessionFactory;


import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.NestedIOException;


/**
 * 解决 Mybatis 的 xml 文件语法错误时不打印错误的问题
 * @author viaco
 */
public class SecureSqlSessionFactoryBean extends SqlSessionFactoryBean {
    public static Logger log = Logger.getLogger(SecureSqlSessionFactoryBean.class.getName());
    private static int logSum =0;
    private static int logSumMax=3;
    @Override
    protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
        try {
            return super.buildSqlSessionFactory();
        } catch (NestedIOException e) {
            //e.printStackTrace(); // XML 有错误时打印异常。
            if(logSum<logSumMax){
                log.error("##SecureSqlSessionFactoryBean## ",e);
                logSum++;
            }

            throw new NestedIOException("Failed to parse mapping resource: '" , e);
        } finally {
            ErrorContext.instance().reset();
        }
    }


}